#import "ABZoomSliderCell.h"
#import "ABZoomSliderFloaterView.h"

//Gotta set default size values

static CGFloat pointerLength = 4.0;

@implementation ABZoomSliderCell

- (void)mouseDown:(NSEvent *)theEvent
{
	[_floater orderFront:nil];
}

- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
{
	[_floater orderOut:nil];
	[super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
}

- (void) awakeFromNib 
{
	left = (NSImage *)[[NSImage imageNamed:@"sliderBG_Left.png"] retain];
	right = (NSImage *)[[NSImage imageNamed:@"sliderBG_Right.png"] retain];
	center = (NSImage *)[[NSImage imageNamed:@"sliderBG_Center.png"] retain];
	
	_floaterView = [[ABZoomSliderFloaterView alloc] initWithFrame:NSMakeRect(0,0,64,32)];
	
	[_floaterView setSliderCell:self];
	
	_floater = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0,0, 64, 32) styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered  defer:NO] retain];
	[_floater setBackgroundColor:[NSColor clearColor]];
	[_floater setOpaque:NO];
	[_floater setContentView:_floaterView];
	[_floater setHasShadow:YES];
	
	innerGradient = (NSGradient*)[[[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.7 alpha:1.0] endingColor:[NSColor whiteColor]] retain];
}

- (void)drawKnob:(NSRect)knobRect
{	
	NSRect knobImageRect = NSMakeRect(knobRect.origin.x+(knobRect.size.width/2)-(7/2) ,knobRect.origin.y, 6 , 14);
	
	NSBezierPath *knobPath = (NSBezierPath *)[NSBezierPath bezierPathWithRect:NSMakeRect(knobImageRect.origin.x, knobImageRect.origin.y+pointerLength, knobImageRect.size.width,  knobImageRect.size.height-pointerLength)];

	[knobPath moveToPoint:NSMakePoint([knobPath bounds].origin.x, knobImageRect.origin.y+pointerLength)];
	[knobPath lineToPoint:NSMakePoint([knobPath bounds].origin.x+[knobPath bounds].size.width/2, knobImageRect.origin.y)];
	[knobPath lineToPoint:NSMakePoint([knobPath bounds].origin.x+[knobPath bounds].size.width, knobImageRect.origin.y+pointerLength)];

	NSBezierPath *knobInnerPath = (NSBezierPath *)[NSBezierPath bezierPathWithRect:NSMakeRect([knobPath bounds].origin.x+1,[knobPath bounds].origin.y+pointerLength+1,4,8)];

	[knobInnerPath moveToPoint:NSMakePoint([knobInnerPath bounds].origin.x, knobImageRect.origin.y+pointerLength+1)];
	[knobInnerPath lineToPoint:NSMakePoint([knobInnerPath bounds].origin.x+[knobInnerPath bounds].size.width/2, knobImageRect.origin.y+2)];
	[knobInnerPath lineToPoint:NSMakePoint([knobInnerPath bounds].origin.x+[knobInnerPath bounds].size.width, knobImageRect.origin.y+pointerLength+1)];
		
	[[NSColor colorWithCalibratedWhite:0.3 alpha:1.0] set];

	[knobPath fill];
	[innerGradient drawInBezierPath:(NSBezierPath *)knobInnerPath angle:(CGFloat)-90.0];
	[_floater setFrameOrigin:[[[self controlView] window] convertBaseToScreen:NSMakePoint([[self controlView] frame].origin.x+knobRect.origin.x-23, [[self controlView] frame].origin.y+knobRect.origin.y+20+6)]];
	[[_floater contentView] setNeedsDisplay:YES];
}

- (BOOL)_usesCustomTrackImage {
    return YES;
}

- (void)_drawCustomTrackWithTrackRect:(NSRect)trackRect inView:(NSView *)controlView 
{
		[left setFlipped:YES];
		[right setFlipped:YES];
		[center setFlipped:YES];

		[left drawInRect:NSMakeRect(0,NSMidY(trackRect)-3,4,5) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
		[center drawInRect:NSMakeRect(4,NSMidY(trackRect)-3,trackRect.size.width-8,5) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
		[right drawInRect:NSMakeRect(trackRect.size.width-4,NSMidY(trackRect)-3,4,5) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}

@end
